package com.sun.module_network

import okhttp3.OkHttpClient
import retrofit2.Retrofit

/**
 *@author:sunc
 *@time :2025/7/18 13:57
 *@desc: 描述
 */
abstract class BaseNetWorkApi {

    /**
     * okHttpClient
     */
    private val okHttpClient: OkHttpClient
        get() {
            var builder = OkHttpClient.Builder()
            builder = setHttpClientBuilder(builder)
            return builder.build()
        }

    fun <T> getApi(serviceClass: Class<T>, baseUrl: String): T {
        val retrofitBuilder = Retrofit.Builder()
            .baseUrl(baseUrl)
            .client(okHttpClient)

        return setRetrofitBuilder(retrofitBuilder).build().create(serviceClass)
    }

    /**
     * 实现重写父类setHttpClientBuilder方法
     * 在这里可以添加拦截器，可以对 OkHttpClient.Builder 做任意操作
     * @param builder  OkHttpClient.Builder
     * @return OkHttpClient.Builder
     */
    abstract fun setHttpClientBuilder(builder: OkHttpClient.Builder): OkHttpClient.Builder

    /**
     * 实现重写父类的setRetrofitBuilder方法，
     * 在这里可以对Retrofit.Builder做任意操作，比如添加GSON解析器，Protocol
     * @param builder  Retrofit.Builder
     * @return Retrofit.Builder
     */
    abstract fun setRetrofitBuilder(builder: Retrofit.Builder): Retrofit.Builder


}